/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.*;
import java.awt.Component;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.*;
import dao.nhanvienDAO;
import pojo.Nhanvien;

/**
 *
 * @author HoaDinhGiang
 */
public class PhieuXuatHangInternalFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form PhieuXuatHangInternalFrame
     */
    private DefaultComboBoxModel cbmCuaHang;
    private DefaultComboBoxModel cbmHinhThucThanhToan;
    private DefaultComboBoxModel cbmTrangThaiDonHang;
    private DefaultComboBoxModel cbmKhoHang;
    private DefaultComboBoxModel cbmDieuKienThanhToan;
    private List<Phieuxuathang> dsPhieuXuat;
    private Phieuxuathang _TeampObjectPhieuXuat;
    private DefaultTableModel dtm;
    protected  Nhanvien _NhanVienPXH;
    public PhieuXuatHangInternalFrame() {
        initComponents();
        dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Nhân Viên");
        dtm.addColumn("Cửa Hàng");
        dtm.addColumn("Hình Thức Thanh Toán");
        dtm.addColumn("Điều Kiện Thanh Toán");
        dtm.addColumn("Trạng Thái Đơn Hàng");
        dtm.addColumn("Kho Hàng");
        dtm.addColumn("Ngày Tháng");
        dtm.addColumn("Tổng Tiền");

    }
    public void hienThiPhieuXuatTonTai() {

        jtbPhieuXuatHang.removeAll();
        dsPhieuXuat = phieuxuatDAO.layDanhSachPhieuXuatTonTai();
        //Tạo model cho Table


        dtm.setNumRows(dsPhieuXuat.size());
        for (int i = 0; i < dsPhieuXuat.size(); i++) {
            Phieuxuathang hh = dsPhieuXuat.get(i);
            dtm.setValueAt(hh.getMaphieuxuathang(), i, 0);
           
            dtm.setValueAt(hh.getNhanvien().getTennhanvien(), i, 1);
            dtm.setValueAt(hh.getCuahang().getTencuahang(), i, 2);
            dtm.setValueAt(hh.getHinhthucthanhtoan().getTenhinhthucthanhtoan(), i, 3);
            dtm.setValueAt(hh.getDieukienthanhtoan().getTendieukienthanhtoan(), i, 4);
            dtm.setValueAt(hh.getTrangthaidonhang().getTentrangthaidonhang(), i, 5);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 6);
            dtm.setValueAt(hh.getNgaythang(), i, 7);
            dtm.setValueAt(hh.getTongtien(), i, 8);
        }
        //Hiển thị lên Table
        this.jtbPhieuXuatHang.setModel(dtm);
        this.jtbPhieuXuatHang.changeSelection(0, 0, false, false);

    }
    public void loadcomboxCuaHang() {
        try {
            this.cbmCuaHang = new DefaultComboBoxModel();
            this.jcbCuaHang.setModel(cbmCuaHang);
            List<Cuahang> ds = cuahangDAO.layDanhSachCuaHangTonTai();
            for (int i = 0; i < ds.size(); i++) {
                Cuahang ch = ds.get(i);
                this.cbmCuaHang.addElement(ch);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void loadcomboxHinhThucThanhToan() {
        try {
            this.cbmHinhThucThanhToan = new DefaultComboBoxModel();
            this.jcbHinhThucThanhToan.setModel(cbmHinhThucThanhToan);
            List<Hinhthucthanhtoan> ds = hinhthucthanhtoanDAO.layDanhSachHTTT();
            for (int i = 0; i < ds.size(); i++) {
                Hinhthucthanhtoan ch = ds.get(i);
                this.cbmHinhThucThanhToan.addElement(ch);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void loadcomboxDieuKienThanhToan() {
        try {
            this.cbmDieuKienThanhToan = new DefaultComboBoxModel();
            this.jcbDieuKienThanhToan.setModel(cbmDieuKienThanhToan);
            List<Dieukienthanhtoan> ds = dieukienthanhtoanDAO.layDanhSachDKTT();
            for (int i = 0; i < ds.size(); i++) {
                Dieukienthanhtoan ch = ds.get(i);
                this.cbmDieuKienThanhToan.addElement(ch);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void loadcomboxKhoHang() {
        try {
            this.cbmKhoHang = new DefaultComboBoxModel();
            this.jcbKhoHang.setModel(cbmKhoHang);
            List<Khohang> ds = khohangDAO.layDanhSachKhoHang();
            for (int i = 0; i < ds.size(); i++) {
                Khohang ch = ds.get(i);
                this.cbmKhoHang.addElement(ch);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void loadcomboxTrangThaiDonHang() {
        try {
            this.cbmTrangThaiDonHang = new DefaultComboBoxModel();
            this.jcbTrangThaiDonHang.setModel(cbmTrangThaiDonHang);
            List<Trangthaidonhang> ds = trangthaidonhangDAO.layDanhSachTTDH();
            for (int i = 0; i < ds.size(); i++) {
                Trangthaidonhang ch = ds.get(i);
                this.cbmTrangThaiDonHang.addElement(ch);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane2 = new javax.swing.JDesktopPane();
        jtfMaPhieuXuat = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jbtLapPhieuXuat = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbPhieuXuatHang = new javax.swing.JTable();
        jdcngaynhap = new com.toedter.calendar.JDateChooser();
        jtfTenNhanVien = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbCuaHang = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jcbHinhThucThanhToan = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jcbDieuKienThanhToan = new javax.swing.JComboBox();
        jcbTrangThaiDonHang = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jcbKhoHang = new javax.swing.JComboBox();
        jbtThoat = new javax.swing.JButton();
        jbtLoad = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Phiếu Xuất Hàng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jDesktopPane2.setBackground(new java.awt.Color(240, 240, 240));
        jtfMaPhieuXuat.setBounds(160, 20, 230, 20);
        jDesktopPane2.add(jtfMaPhieuXuat, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("Mã Phiếu Xuất:");
        jLabel1.setBounds(10, 20, 120, 14);
        jDesktopPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbtLapPhieuXuat.setText("Lập Phiếu Xuất Hàng");
        jbtLapPhieuXuat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLapPhieuXuatActionPerformed(evt);
            }
        });
        jbtLapPhieuXuat.setBounds(580, 480, 190, 30);
        jDesktopPane2.add(jbtLapPhieuXuat, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Phiếu Xuất Hàng"));

        jtbPhieuXuatHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbPhieuXuatHang);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 878, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
        );

        jPanel1.setBounds(0, 172, 890, 300);
        jDesktopPane2.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jdcngaynhap.setDateFormatString("dd/MM/yyyy");
        jdcngaynhap.setBounds(160, 60, 230, 20);
        jDesktopPane2.add(jdcngaynhap, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jtfTenNhanVien.setEnabled(false);
        jtfTenNhanVien.setBounds(160, 90, 230, 20);
        jDesktopPane2.add(jtfTenNhanVien, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Nhân Viên:");
        jLabel3.setBounds(10, 100, 90, 14);
        jDesktopPane2.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Ngày Tháng:");
        jLabel4.setBounds(10, 60, 100, 14);
        jDesktopPane2.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Cửa Hàng:");
        jLabel2.setBounds(10, 140, 90, 14);
        jDesktopPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jcbCuaHang.setBounds(160, 130, 230, 20);
        jDesktopPane2.add(jcbCuaHang, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Hình Thức Thanh Toán:");
        jLabel7.setBounds(440, 20, 160, 14);
        jDesktopPane2.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jcbHinhThucThanhToan.setBounds(650, 20, 230, 20);
        jDesktopPane2.add(jcbHinhThucThanhToan, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Điều Kiện Thanh Toán:");
        jLabel6.setBounds(440, 60, 160, 14);
        jDesktopPane2.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jcbDieuKienThanhToan.setBounds(650, 60, 230, 20);
        jDesktopPane2.add(jcbDieuKienThanhToan, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jcbTrangThaiDonHang.setBounds(650, 100, 230, 20);
        jDesktopPane2.add(jcbTrangThaiDonHang, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Trạng Thái Đơn Hàng:");
        jLabel5.setBounds(440, 100, 150, 14);
        jDesktopPane2.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setText("Kho Hàng:");
        jLabel8.setBounds(440, 140, 110, 14);
        jDesktopPane2.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jcbKhoHang.setBounds(650, 140, 230, 20);
        jDesktopPane2.add(jcbKhoHang, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });
        jbtThoat.setBounds(810, 480, 70, 30);
        jDesktopPane2.add(jbtThoat, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbtLoad.setText("Cập Nhật");
        jbtLoad.setActionCommand("");
        jbtLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLoadActionPerformed(evt);
            }
        });
        jbtLoad.setBounds(10, 480, 90, 30);
        jDesktopPane2.add(jbtLoad, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 893, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        loadcomboxDieuKienThanhToan();
        loadcomboxCuaHang();
        loadcomboxHinhThucThanhToan();
        loadcomboxTrangThaiDonHang();
        jcbTrangThaiDonHang.setSelectedIndex(1);
        loadcomboxKhoHang();
        String str = this._NhanVienPXH.getTennhanvien();
        jtfTenNhanVien.setText(str);
        hienThiPhieuXuatTonTai();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtLapPhieuXuatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLapPhieuXuatActionPerformed
        // TODO add your handling code here:
        //Them 1 Phieu xuat moi vao co so du lieu
        Phieuxuathang pxh = new Phieuxuathang();
        pxh.setNhanvien(_NhanVienPXH);
        Cuahang ch = (Cuahang) jcbCuaHang.getItemAt(jcbCuaHang.getSelectedIndex());
        pxh.setCuahang(ch);
        Hinhthucthanhtoan httt = (Hinhthucthanhtoan) jcbHinhThucThanhToan.getItemAt(jcbHinhThucThanhToan.getSelectedIndex());
        pxh.setHinhthucthanhtoan(httt);
        Dieukienthanhtoan dktt = (Dieukienthanhtoan) jcbDieuKienThanhToan.getItemAt(jcbDieuKienThanhToan.getSelectedIndex());
        pxh.setDieukienthanhtoan(dktt);
        Trangthaidonhang ttdh = (Trangthaidonhang) jcbTrangThaiDonHang.getItemAt(jcbTrangThaiDonHang.getSelectedIndex());
        pxh.setTrangthaidonhang(ttdh);
        Khohang kh = (Khohang) jcbKhoHang.getItemAt(jcbKhoHang.getSelectedIndex());
        pxh.setKhohang(kh);
        pxh.setKichhoat(true);
        pxh.setTongtien(0.0);
        pxh.setNgaythang(jdcngaynhap.getDate());
        
        boolean kq = phieuxuatDAO.themPhieuXuat(pxh);
        if (kq == true) {
            hienThiPhieuXuatTonTai();
            
        } else {
            JOptionPane.showMessageDialog(this, "Xuất hàng thất bại");
        }
        
        //-Lap chi tiet phieu xuat hang
        ChiTietPhieuXuatInternalFrame frm = new ChiTietPhieuXuatInternalFrame();
        Component add = this.jDesktopPane2.add(frm);
        try {
            frm.setMaximum(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Gán mã phiếu xuất mới cho chi tiết xuất hàng
        //Cần lấy ra mã PX mới thiết lập
        frm._phieuXuatHang = pxh;
        frm.setVisible(true);
    }//GEN-LAST:event_jbtLapPhieuXuatActionPerformed

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
        // TODO add your handling code here:
     //   hienThiPhieuXuatTonTai();
    }//GEN-LAST:event_formInternalFrameActivated

    private void jbtLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLoadActionPerformed
        // TODO add your handling code here:
        hienThiPhieuXuatTonTai();
    }//GEN-LAST:event_jbtLoadActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane jDesktopPane2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtLapPhieuXuat;
    private javax.swing.JButton jbtLoad;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JComboBox jcbCuaHang;
    private javax.swing.JComboBox jcbDieuKienThanhToan;
    private javax.swing.JComboBox jcbHinhThucThanhToan;
    private javax.swing.JComboBox jcbKhoHang;
    private javax.swing.JComboBox jcbTrangThaiDonHang;
    private com.toedter.calendar.JDateChooser jdcngaynhap;
    private javax.swing.JTable jtbPhieuXuatHang;
    private javax.swing.JTextField jtfMaPhieuXuat;
    private javax.swing.JTextField jtfTenNhanVien;
    // End of variables declaration//GEN-END:variables
}
